﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Reflection;

namespace HDJ.Framework.Core
{
    public static class TableManager
    {
        public class TableClassManager<T> where T : TableBase
        {
            private static T[] allTableClassData = null;
            private static TableData tableData;

            public static T[] GetDataArrayList(TableData cData)
            {
                if (allTableClassData == null || tableData != cData)
                {
                    allTableClassData = TableUtils.CreateTableClass<T>(cData);
                    tableData = cData;
                }
                else
                {
                    return allTableClassData;
                }

                return allTableClassData;
            }
        }

        private static Dictionary<string, TableData> allTableConfigData = new Dictionary<string, TableData>();
        /// <summary>
        /// 获取全部配置数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName">当fileName为空或null时，默认使用类名作为文件名加载</param>
        /// <returns></returns>
        public static T[] GetConfigArray<T>(string fileName = null) where T : TableBase
        {
            Type type = typeof(T);
            if (string.IsNullOrEmpty(fileName))
                fileName = type.Name;
            TableData cData = GetTableData(fileName);
            return TableClassManager<T>.GetDataArrayList(cData);           
        }
        /// <summary>
        /// 获取一条配置数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T GetConfig<T>(string key, string fileName = null) where T : TableBase
        {
            T[] tArr = GetConfigArray<T>(fileName);
            foreach (var item in tArr)
            {
                if (item.CompareKey(key))
                    return item;
            }
            return null;
        }
        /// <summary>
        /// 加载并解析配置信息
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="isOnlyLoadClassData">是否只加载能转换成 TableConfigBase 的数据（运行时使用可优化性能）</param>
        /// <param name="type">继承TableConfigBase的class的type</param>
        /// <returns></returns>
        public static TableData GetTableData(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return null;
            TableData data = null;
            if (!allTableConfigData.ContainsKey(fileName))
            {
                string strData = ResourceManager.LoadText(fileName);
                //Debug.Log("Lenth" + temp1.Length);
                data = TableUtils.TableStringToTableData(strData);
                allTableConfigData.Add(fileName, data);
            }
            else
                data = allTableConfigData[fileName];
            return data;
        }
        public static void Clear()
        {
            List<string> list = new List<string>(allTableConfigData.Keys);
            foreach (var item in list)
            {
                Release(item);
            }

            allTableConfigData.Clear();
        }
        public static void Release(string fileName)
        {
            if (allTableConfigData.ContainsKey(fileName))
            {
                ResourceManager.Release(fileName);
                allTableConfigData.Remove(fileName);
            }

        }
      
    }
}
